package cn.xfdzx.android.apps.shop.adapter;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.bean.ShopCarListBeen;
import cn.xfdzx.android.apps.shop.bean.StoreGoodsListBeen;
import cn.xfdzx.android.apps.shop.contract.ItemClickListener;
import cn.xfdzx.android.apps.shop.presenter.ShopCarListPresenter;
import cn.xfdzx.android.apps.shop.util.Utils;
import cn.xfdzx.android.apps.shop.video.VideoShopGoodActivity;

/**
 * @author gzk
 * @date 2020/4/13 0013
 * description：直播中添加商品
 */
public class ItemVideoGoodAdapter extends RecyclerView.Adapter<ItemVideoGoodAdapter.MyViewCarRetailHolder> {

    private ItemClickListener mItemClickListener;
    private List<StoreGoodsListBeen.DataBean.GoodsBean> mList;
    private List<String> ids;
    private VideoShopGoodActivity mContext;

    private final LayoutInflater mLayoutInflater;



    public ItemVideoGoodAdapter(VideoShopGoodActivity context, List<StoreGoodsListBeen.DataBean.GoodsBean> list,List<String> ids) {
        this.mList = list;
        this.mContext = context;
        this.ids=ids;
        //以后用它来初始化布局
        mLayoutInflater = LayoutInflater.from(mContext);
    }


    @NonNull
    @Override
    public MyViewCarRetailHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_video_good_list, parent, false);
        return new MyViewCarRetailHolder(mContext, inflate);

    }

    @Override
    public void onBindViewHolder(@NonNull MyViewCarRetailHolder holder, int position) {

        RoundedCorners roundedCorners = new RoundedCorners(20);
        //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
        RequestOptions options = RequestOptions.bitmapTransform(roundedCorners).override(200, 200);
        options.placeholder(R.drawable.icon_defult)//图片加载出来前，显示的图片
                .fallback(R.drawable.icon_defult) //url为空的时候,显示的图片
                .error(R.drawable.icon_defult);
        Glide.with(mContext).load(mList.get(position).getThumb() ).apply(options.transform(new CenterCrop(), roundedCorners)).into(holder.ivShopPicture);
        holder.tvShopGoodsName.setText(mList.get(position).getTitle() );
        holder.tvGoodsNum.setText("库存:"+mList.get(position).getStock() + "");

        if (Double.parseDouble(mList.get(position).getDiscountprice()) == 0) {
            holder.tvShopGoodsPrice.setText("¥" + mList.get(position).getMarketprice() + "");
        } else {
            holder.tvShopGoodsPrice.setText("¥" + mList.get(position).getDiscountprice()  + "");
        }
        if (mList.get(position).isIs_select()) {
            mList.get(position).setIs_select(true);
            holder.ivShopSelect.setImageResource(R.drawable.icon_select_orange);

            mContext.heji();
        } else {
            mList.get(position).setIs_select(false);
            holder.ivShopSelect.setImageResource(R.drawable.d99999_oval);

            mContext.heji();
        }
        holder.ivShopSelect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (mList.get(position).isIs_select()) {
                    mList.get(position).setIs_select(false);
                    holder.ivShopSelect.setImageResource(R.drawable.d99999_oval);
                    if (ids.contains(mList.get(position).getId()+"")){
                        ids.remove(mList.get(position).getId()+"");

                    }
                } else {
                    if (!ids.contains(mList.get(position).getId()+"")){
                        ids.add(mList.get(position).getId()+"");

                    }
                    mList.get(position).setIs_select(true);
                    holder.ivShopSelect.setImageResource(R.drawable.icon_select_orange);
                }
                notifyDataSetChanged();
            }
        });
    }


    @Override
    public int getItemCount() {
        return mList.size();
    }

    class MyViewCarRetailHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.iv_shop_select)
        ImageView ivShopSelect;
        @BindView(R.id.iv_shop_picture)
        ImageView ivShopPicture;
        @BindView(R.id.tv_shop_goods_name)
        TextView tvShopGoodsName;
        @BindView(R.id.tv_shop_goods_price)
        TextView tvShopGoodsPrice;

        @BindView(R.id.tv_goods_num)
        TextView tvGoodsNum;

        private Context mContext;


        public MyViewCarRetailHolder(Context mContext, View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            this.mContext = mContext;
        }


    }


    public void setOnItemClickListener(ItemClickListener itemClickListener) {
        this.mItemClickListener = itemClickListener;

    }


}
